package com.yusheng.domain;

import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serializable;
import java.util.HashMap;


@EqualsAndHashCode(callSuper = true)
@Data
public class R<T> extends HashMap<String, Object> implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 成功
     */
    public static final int SUCCESS = 200;

    /**
     * 失败
     */
    public static final int FAIL = 500;

    private int code;

    private String message;

    private T data;

    public static R<?> ok() {
        return restResult("", SUCCESS, "");
    }

    public static <T> R<T> ok(T data) {
        return restResult(data, SUCCESS, "");
    }

    public static <T> R<T> ok(T data, String msg) {
        return restResult(data, SUCCESS, msg);
    }

    public static R<?> fail() {
        return restResult("", FAIL, "");
    }

    public static R<?> fail(String msg) {
        return restResult("", FAIL, msg);
    }

    public static <T> R<T> fail(T data) {
        return restResult(data, FAIL, "");
    }


    public static R<?> fail(int code, String msg) {
        return restResult("", code, msg);
    }

    public static <T> R<T> restResult(T data, int code, String msg) {
        R<T> apiResult = new R<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMessage(msg);
        return apiResult;
    }
}

